package com.yfqy.admin.resource;

import com.github.pagehelper.PageInfo;
import com.yfqy.admin.domain.dto.QueryById;
import com.yfqy.admin.domain.dto.system.AddMenusToRole;
import com.yfqy.admin.domain.dto.system.AddRole;
import com.yfqy.admin.domain.dto.system.AddRolesToUser;
import com.yfqy.admin.domain.dto.system.AddSysMenu;
import com.yfqy.admin.domain.dto.system.AddSysUser;
import com.yfqy.admin.domain.dto.system.MenusWithRole;
import com.yfqy.admin.domain.dto.system.ModifySysMenu;
import com.yfqy.admin.domain.dto.system.ModifySysRole;
import com.yfqy.admin.domain.dto.system.ModifySysUser;
import com.yfqy.admin.domain.dto.system.RolesWithUser;
import com.yfqy.admin.domain.dto.system.SysMenuDetail;
import com.yfqy.admin.domain.dto.system.SysMenuQuery;
import com.yfqy.admin.domain.dto.system.SysRoleDetail;
import com.yfqy.admin.domain.dto.system.SysRoleQuery;
import com.yfqy.admin.domain.dto.system.SysUserDetail;
import com.yfqy.admin.domain.dto.system.SysUserQuery;
import com.yfqy.admin.domain.vo.common.ResultVO;
import com.yfqy.admin.service.SystemManagerService;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 系统管理（账号角色权限）
 */
@RestController
@RequestMapping("/api/internal/system/manager")
public class SystemManagerController {

    @Resource
    private SystemManagerService systemManagerService;

    //--------------------sysUser----------------

    /**
     * 添加系统管理员
     */
    @PostMapping("/addSysUser")
    @PreAuthorize("hasAuthority('user:add')")
    public ResultVO<String> addSysUser(@RequestBody @Validated AddSysUser addSysUser) {
        return systemManagerService.addSysUser(addSysUser);
    }

    /**
     * 系统用户详情
     */
    @PostMapping("/getSysUserById")
    public ResultVO<SysUserDetail> getSysUserById(@RequestBody @Validated QueryById queryById) {
        return systemManagerService.getSysUserById(queryById.getId());
    }

    /**
     * 系统用户分页列表
     */
    @PostMapping("/getSysUserPage")
    @PreAuthorize("hasAuthority('user:list')")
    public ResultVO<PageInfo<SysUserDetail>> getSysUserPage(@RequestBody SysUserQuery sysUserQuery) {
        return systemManagerService.getSysUserPage(sysUserQuery);
    }

    /**
     * 编辑修改系统用户
     */
    @PostMapping("/modifySysUser")
    @PreAuthorize("hasAuthority('user:modify')")
    public ResultVO<Void> modifySysUser(@RequestBody @Validated ModifySysUser modifySysUser) {
        return systemManagerService.modifySysUser(modifySysUser);
    }

    /**
     * 删除系统用户
     */
    @PostMapping("/deleteSysUser")
    @PreAuthorize("hasAuthority('user:delete')")
    public ResultVO<String> deleteSysUser(@RequestBody @Validated QueryById queryById) {
        return systemManagerService.deleteSysUser(queryById.getId());
    }

    /**
     * 给系统用户分配角色
     */
    @PostMapping("/addRolesToUser")
    public ResultVO<String> addRolesToUser(@RequestBody @Validated AddRolesToUser addRolesToUser) {
        return systemManagerService.addRolesToUser(addRolesToUser);
    }

    /**
     * 用户绑定角色回写展示
     */
    @PostMapping("/rolesWithUser")
    public ResultVO<List<RolesWithUser>> rolesWithUser(@RequestBody @Validated QueryById QueryById) {
        return systemManagerService.rolesWithUser(QueryById);
    }


    //-------------------sysRole--------------------

    /**
     * 添加系统角色
     */
    @PostMapping("/addSysRole")
    @PreAuthorize("hasAuthority('role:add')")
    public ResultVO<String> addSysRole(@RequestBody @Validated AddRole addRole) {
        return systemManagerService.addSysRole(addRole);
    }

    /**
     * 系统角色详情
     */
    @PostMapping("/getSysRoleById")
    public ResultVO<SysRoleDetail> getSysRoleById(@RequestBody @Validated QueryById queryById) {
        return systemManagerService.getSysRoleById(queryById.getId());
    }

    /**
     * 系统角色分页列表
     */
    @PostMapping("/getSysRolePage")
    @PreAuthorize("hasAnyAuthority('role:list', 'role:add', 'role:modify')")
    public ResultVO<PageInfo<SysRoleDetail>> getSysRolePage(@RequestBody SysRoleQuery sysRoleQuery) {
        return systemManagerService.getSysRolePage(sysRoleQuery);
    }

    /**
     * 编辑修改系统角色
     */
    @PostMapping("/modifySysRole")
    @PreAuthorize("hasAnyAuthority('role:add', 'role:modify')")
    public ResultVO<String> modifySysRole(@RequestBody @Validated ModifySysRole modifySysRole) {
        return systemManagerService.modifySysRole(modifySysRole);
    }

    /**
     * 删除系统角色
     */
    @PostMapping("/deleteSysRole")
    @PreAuthorize("hasAnyAuthority('role:add', 'role:modify')")
    public ResultVO<String> deleteSysRole(@RequestBody @Validated QueryById queryById) {
        return systemManagerService.deleteSysRole(queryById.getId());
    }

    /**
     * 给系统角色分配菜单权限
     */
    @PostMapping("/addMenusToRole")
    public ResultVO<String> addMenusToRole(@RequestBody @Validated AddMenusToRole addMenusToRole) {
        return systemManagerService.addMenusToRole(addMenusToRole);
    }

    /**
     * 角色绑定菜单权限回写展示
     */
    @PostMapping("/menusWithRole")
    public ResultVO<List<MenusWithRole>> menusWithRole(@RequestBody @Validated QueryById queryById) {
        return systemManagerService.menusWithRole(queryById);
    }

    //-------------------sysMenu------------------------

    /**
     * 添加系统菜单（目录菜单按钮）
     */
    // @PreAuthorize("hasAuthority('menu:add')")
    @PostMapping("/addSysMenu")
    public ResultVO<Long> addSysMenu(@RequestBody @Validated AddSysMenu addSysMenu) {
        return systemManagerService.addSysMenu(addSysMenu);
    }

    /**
     * 系统菜单详情
     */
    @PostMapping("/getSysMenuById")
    public ResultVO<SysMenuDetail> getSysMenuById(@RequestBody @Validated QueryById queryById) {
        return systemManagerService.getSysMenuById(queryById.getId());
    }

    /**
     * 系统菜单列表
     */
    @PostMapping("/getSysMenuPage")
    public ResultVO<PageInfo<SysMenuDetail>> getSysMenuPage(@RequestBody SysMenuQuery sysMenuQuery) {
        return systemManagerService.getSysMenuPage(sysMenuQuery);
    }

    /**
     * 系统菜单Tree表
     */
    @PostMapping("/getSysMenuTree")
    public ResultVO<List<SysMenuDetail>> getSysMenuTree() {
        return systemManagerService.getSysMenuTree();
    }

    /**
     * 用户菜单Tree表
     */
    @PostMapping("/getSysUserMenuTree")
    public ResultVO<List<SysMenuDetail>> getSysUserMenuTree() {
        return systemManagerService.getSysUserMenuTree();
    }

    /**
     * 编辑修改系统菜单权限
     */
    @PostMapping("/modifySysMenu")
    public ResultVO<Void> modifySysMenu(@RequestBody @Validated ModifySysMenu modifySysMenu) {
        return systemManagerService.modifySysMenu(modifySysMenu);
    }

    /**
     * 删除系统菜单权限
     */
    @PostMapping("/deleteSysMenu")
    public ResultVO<Void> deleteSysMenu(@RequestBody @Validated QueryById queryById) {
        return systemManagerService.deleteSysMenu(queryById.getId());
    }

}
